using KL.Standard.Domain.Repositories;
using KL.Standard.Domain.Services;
using KL.Standard.Model.Infrastructure;
using KL.Standard.Model.Models.BaseArch;
using KL.Standard.Model.ViewModels.InParams;
using KL.Standard.Model.ViewModels.OutResult;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace EagCentralPlatform.Net8.Controllers.BaseArch
{
    /// <summary>
    /// 用户信息相关接口
    /// </summary>
    [Route("user")]
    [ApiController]
    public class UserController : BaseApiController
    {
        private readonly IUserSvc _userSvc;

        private readonly IUserRepository _userRepository;

        public UserController(IUserSvc userSvc, IUserRepository userRepository)
        {
            this._userRepository = userRepository;
            this._userSvc = userSvc;
        }

        #region 用户列表
        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("")]
        public async Task<PageResult<AccountVM>> List([FromBody] Dictionary<string, string> param)
        {

            var sarchInfo = new PageSearchModel(param);
            var userInfo = this.GetCurUserInfo();
            if (userInfo.isAdmin(out string companyCode))
            {
                if (!string.IsNullOrEmpty(companyCode))
                    param.Add("companyCode", companyCode);
            }
            // _log.Debug("invoke /user/List:" + sarchInfo);
            var res = await _userSvc.SearchPage(sarchInfo);
            // res.CreateColumnInfos();
            return res;
        }
        #endregion


        #region 用户保存
        /// <summary>
        /// 用户保存
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPut("")]
        public BaseResult<string> Save([FromBody] SysAccountPO param)
        {
            //_log.Debug("invoke /user/Save:" + param);
            if (string.IsNullOrEmpty(param.loginID)
                || string.IsNullOrEmpty(param.mobile) || string.IsNullOrEmpty(param.email))
                return new BaseResult<string>()
                {
                    success = false,
                    error = "参数缺失！"
                };
            else
            {
                var userInfo = GetCurUserInfo();
                param.creatorID = userInfo.id;
                return _userSvc.Save(param);
            }
        }
        #endregion

        #region 用户禁用与开启
        /// <summary>
        /// 用户禁用与开启
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("lock/{loginID}")]
        public BaseResult<string> Lock(string loginID)
        {
            int count = _userRepository.Lock(loginID);
            return new BaseResult<string>()
            {
                success = count > 0,
                error = count > 0?"操作成功！":"操作失败！"
            };
            
        }
        #endregion

        #region 用户重置密码
        /// <summary>
        /// 用户重置密码
        /// </summary>
        /// <param name="idsIdvm"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("restpwd")]
        public BaseResult<string> RestPwd([FromBody] MutlIDVM idsIdvm)
        {
            if (!ModelState.IsValid)
                return new BaseResult<string>()
                {
                    success = false,
                    error = ""
                };
            else
            {
                string[] loginID_array = idsIdvm.idList();
                int count = _userRepository.RestPwd(loginID_array);
                return new BaseResult<string>()
                {
                    success = count > 0,
                    error = count > 0?"操作成功！":"操作失败！"
                };
            }
            //return null;
        }
        #endregion

        #region 用户删除
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="idsIdvm"></param>
        /// <returns></returns>
        [HttpDelete]
        public BaseResult<string> Delete([FromBody] MutlIDVM idsIdvm)
        {
            if (!ModelState.IsValid)
                return new BaseResult<string>()
                {
                    success = false,
                    error = ""
                };
            else
            {
                string[] loginID_array = idsIdvm.idList();
                int count = _userRepository.Delete(loginID_array);
                return new BaseResult<string>()
                {
                    success = count > 0,
                    error = count > 0?"操作成功！":"操作失败！"
                };
            }

        }
        #endregion

        #region 用户保存(中台)
        /// <summary>
        /// 用户保存
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Obsolete("由webapi改为mq消费")]
        [AllowAnonymous]
        [HttpPut]
        [Route("save4zt")]
        public BaseResult<string> Save4ZT([FromBody] SysAccountPO param)
        {
            //_log.Debug("invoke /user/Save:" + param);
            if (string.IsNullOrEmpty(param.loginID)
                || param.roleID == 0 || param.companyID == 0 || param.deptID == 0)
                return new BaseResult<string>()
                {
                    success = false,
                    error = "参数缺失！"
                };
            else
            {
                //var userInfo = GetCurUserInfo();
                param.creatorID = 0;
                return _userSvc.Save4ZT(param);
            }
        }
        #endregion


        #region 用户逻辑删除(中台)
        /// <summary>
        /// 用户逻辑删除(中台)
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        [Obsolete("由webapi改为mq消费")]
        [AllowAnonymous]
        [HttpPut]
        [Route("delete4zt/{loginID}")]
        public BaseResult<string> Delete4ZT(string loginID)
        {
            return _userSvc.Delete4ZT(loginID);

        }
        #endregion
        
        #region 提供当前账户的所有未设置的功能点列表
        /// <summary>
        /// 提供当前账户的所有未设置的功能点列表
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        [Route("unchkmenu4zt/{loginID}")]
        public Task<BaseResult<IEnumerable<TreeNode<string>>>> GetAllUnChkMenus4Account(string  loginID)
        {
            return _userSvc.GetAllUnChkMenus4Account(loginID);
        }
        #endregion

    }
}
